<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<title>Insert title here</title>
<script type="text/javascript">

function modifyItem(seq){ 
	var frm = document.getElementById("frm");
	frm.action = '<c:url value="/board/modifyItem"/>';
	frm.submit();
}

function insertItem(){ 
	var frm = document.getElementById("frm");
	frm.action = '<c:url value="/board/insertItem"/>';
	frm.submit();
}

function replyViewList() { 
	$.ajax({		
		url      : "<c:url value='/board/replyList'/>"
	  , type     : "POST"
	  ,	data     : {
		             "seq" : $("#seq").val()
		           } 
	  ,	datatype : "html"
	  ,	success  : function (result) {
						$("#replyContents").val("");
						$("#replyList").empty();
						$("#replyList").append(result);
					}
	});
}

function replyInsert(){
	
	if($("#replyContents").val() == null || $("#replyContents").val() == ""){
		return false;
	}
	
	$.ajax({
		url     : "<c:url value='/board/replyInsert' />"
	  , type    : "POST"
	  , data    : { 
		              "refSeq"        : $("#seq").val()
			        , "replyContents" : $("#replyContents").val()
		          }
      , dataType :  "html"
      , async    :  true
	  ,	success  :  function (result){
		  				$("#replyContents").val("");
						$("#replyList").html(result);
					}
	});
	
}

function replyDelete(seq){
	
	if (! confirm("삭제 하시겠습니까?")){
		return;
	}
	
	$.ajax({		
		url     : "<c:url value='/board/replyDelete' />"
	  ,	type    : "POST"
	  ,	data    : {
		            seq : seq
		          }
	  ,	success : function(result){
				  	if(result !=null){
						$("#replyList").html(result);
				  	}
				  }
	});
}

$(document).ready(function(){
	replyViewList();
	
	$("#replyInsert").click(function(){
		replyInsert();
		return false;
	});
	
});
</script>

</head>
<body>
<form id="frm" method="post">
<input type="hidden" name="boardCode" value="${boardCode}">
<c:if test="${result.seq ne null}">
<input type="hidden" name="seq" id="seq" value="${result.seq}">
</c:if>
	<table>
		<tr>
			<td>제목</td>
			<td><input type="text" id="bdTitle" name="bdTitle" value='<c:out value="${result.bdTitle}"/>'></td>
		</tr>
		<tr>
			<td>내용</td>
			<td><textarea rows="15" cols="8" id="bdContents" name="bdContents"><c:out value="${result.bdContents }"/></textarea> </td>
		</tr>
	</table>
	
<c:if test="${boardCode eq '3' and seq ne '0'}">
<!-- 덧글 입력 시작 -->
	<br>
	<div id="replyList"> 
		<jsp:include page="replyViewList.jsp"/>
	</div>
	
	<br>
	<div>
		<div style="float: left;">⊙덧글⊙</div>
	</div>
	<br>
	<table border="1" >
		<tr>
			<td>
				<textarea rows="2" cols="70" id="replyContents" name="replyContents"></textarea>
			</td>
			<td>
				<input type="button" id="replyInsert" value="등록">
			</td>
		</tr>
	</table>
	
<!-- 덧글 입력 끝 -->
</c:if>
	
	<c:if test="${result.seq eq null}">
		<input type="button" value="등록" onclick="javascript:insertItem()">
	</c:if>
	
	<c:if test="${result.seq ne null}">
		<input type="button" value="수정" onclick="javascript:modifyItem()">
	</c:if>
	
</form>
</body>
</html>